package cn.lyh.edu.controller;

import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduTeacher;
import cn.lyh.edu.service.IEduTeacherService;
import cn.lyh.edu.service.dto.TeacherQueryCriteria;
import cn.lyh.utils.PageVo;
import cn.lyh.utils.ResultVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**功能描述：教师前端控制器
 */
@RestController
@RequestMapping("teacher")
public class EduTeacherController {

    private final IEduTeacherService eduTeacherService;

    public EduTeacherController(IEduTeacherService eduTeacherService) {
        this.eduTeacherService = eduTeacherService;
    }

//  ("【后台】获取教师列表数据")
    @GetMapping
    public ResponseEntity<Object> getList(TeacherQueryCriteria queryCriteria, PageVo pageVo){
        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id" );
        return new ResponseEntity<>(eduTeacherService.getList(queryCriteria,pageable), HttpStatus.OK);
    }

//   ("【后台】添加教师信息")
    @PostMapping
    public BaseResult addTeacher(@RequestBody EduTeacher eduTeacher){
        eduTeacherService.addTeacher(eduTeacher);
        return BaseResult.success("添加成功");
    }

//   ("【后台】根据ID获取详情信息")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("获取信息失败");
        }
        EduTeacher dbEduTeacher = eduTeacherService.getById(id);
        return BaseResult.success(dbEduTeacher);
    }

//   ("【后台】更新教师信息")
    @PutMapping
    public BaseResult editTeacher(@RequestBody EduTeacher eduTeacher){
        eduTeacherService.editTeacher(eduTeacher);
        return BaseResult.success("更新成功");
    }

//   ("【后台】删除教师信息")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("删除信息失败");
        }else {
            eduTeacherService.deleteById(id);
            return BaseResult.success("删除成功");
        }
    }

//   ("【后台】获取首页教师")
    @GetMapping("all")
    public BaseResult all(){
        List<EduTeacher> list = eduTeacherService.getAll();
        List<ResultVo> resultVoList = list.stream().map(temp-> {
            ResultVo obj = new ResultVo();
            obj.setName(temp.getTeacherName());
            obj.setId(temp.getId());
            return obj;
        }).collect(Collectors.toList());
        return BaseResult.success(resultVoList);
    }

}
